<template>
  <div class="pop-menu" :class="ifShow ? 'show' : ''">
    <div class="menu-content">
      <div class="user-info">
        <i></i>
        <p class="user-name">杜木土</p>
        <p class="user-intro">我姓杜，名木土。</p>
      </div>
      <div class="menu-list">
        <div class="list-item" v-for="(item, index) of menuList" :key="index" @click="turnPage(item)">
          <i></i>
          <span>{{item.name}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PopMenu',
  props: {
    ifShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      menuList: [
        {
          name: '首页',
          icon: '',
          componentName: 'Home'
        },
        {
          name: '归档',
          icon: '',
          componentName: ''
        },
        {
          name: '关于我',
          icon: '',
          componentName: ''
        }
      ]
    }
  },
  methods: {
    turnPage (item) {
      this.$router.push({
        name: item.componentName
      })
      if (item.name === '首页' && this.$route.name === 'Home') {
        this.$emit('emitHandlePop')
      }
    }
  },
  mounted () {
    window.addEventListener('click', (e) => {
      const target = e.target
      const parentNode = document.querySelector('.menu-content')
      if (target.contains(parentNode)) {
        this.$emit('emitHandlePop')
      }
    }, false)
  },
  watch: {
    ifShow: {
      handler: function (val) {
        const mo = function (e) { e.preventDefault() }
        if (val) {
          document.body.style.overflow = 'hidden'
          document.addEventListener('touchmove', mo, false)
        } else {
          document.body.style.overflow = ''
          document.removeEventListener('touchmove', mo, false)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.pop-menu {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0);
  z-index: -1;
  transition: all 0.25s ease-in-out;
  .menu-content {
    width: 100%;
    // height: 2rem;
    background-color: #f5f5f5;
    position: absolute;
    top: -100%;
    right: 0;
    transition: all 0.25s ease-in-out;
    border-bottom-left-radius: 0.2rem;
    border-bottom-right-radius: 0.2rem;
  }
  &.show {
    z-index: 999;
    background: rgba(0, 0, 0, 0.5);
    .menu-content {
      top: 0;
    }
  }
  .user-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.3rem 0;
    box-sizing: border-box;
    i {
      width: 0.65rem;
      height: 0.65rem;
      background: url('../assets/images/avator.jpg') 50% 50% no-repeat;
      background-size: cover;
      border-radius: 50%;
      margin-bottom: 0.15rem;
    }
    p {
      &.user-name {
        font-size: 0.18rem;
        font-weight: bold;
        margin-bottom: 0.03rem;
      }
      &.user-intro {
        color: #a5a5a5;
      }
    }
  }
  .menu-list {
    padding: 0 0.2rem 0.3rem 0.2rem;
    box-sizing: border-box;
    .list-item {
      display: flex;
      align-items: center;
      justify-content: center;
      line-height: 0.5rem;
      border-top: 1px solid #e5f5ee;
      span {
        font-size: 0.16rem;
        color: #666666;
      }
    }
  }
}
</style>
